/*
 * 系统项目名称：综合决策分析系统
 * 文件名：HisDataCache.java
 * 描述：
 * 版权：Copyright 2015-2020 义乌市跨境电商供应链管理有限公司-版权所有
 * 修改人：xushaobing
 * 修改时间：2015年10月29日 下午1:36:34
 * 版本号：V1.1
 */
package com.ywt.cda.service;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ywt.cda.constants.DateConstants;
import com.ywt.cda.domain.Ana1stData;
import com.ywt.cda.domain.Ana2ndData;
import com.ywt.cda.domain.ConfCountry;
import com.ywt.cda.domain.ConfParam;
import com.ywt.cda.domain.Coordinate;
import com.ywt.cda.domain.MCategory;
import com.ywt.cda.domain.SCategory;
import com.ywt.cda.domain.enums.AnaTable;
import com.ywt.cda.domain.enums.DimensionFlag;
import com.ywt.cda.domain.enums.ParamFlag;
import com.ywt.cda.service.vo.ExportPortDataVo;
import com.ywt.cda.service.vo.LogisTypeDataVo;
import com.ywt.cda.utils.DateUtils;

/**
 * （一句话功能简述） <br>
 * （功能详细描述）
 * 
 * @author 徐韶兵
 * @version V1.0
 * @see [相关类/方法]
 * @since 2015年10月29日 下午1:36:34
 */

public class DataCacheService {
	/**
	 * 日志读取器
	 */
	private static final Logger logger = Logger
			.getLogger(DataCacheService.class);

	/**
	 * 数据分析器
	 */
	@Resource
	private GovAnalysisService govAnalysisService;
	/**
	 * 查询结果
	 */
	private JSONObject result;
	/**
	 * 目前为止所有数据的结果
	 */
	private JSONObject recentlyData = new JSONObject();
	/**
	 * 出口总包裹数
	 */
	private BigDecimal totalNum = new BigDecimal(0);

	private BigDecimal totalPrice = new BigDecimal(0);
	/**
	 * 配置数据唯一实例
	 */
	@Resource
	private ConfDataCacheService cdc;
	@Resource
	private RedisTemplate<Serializable, Serializable> redisTemplate;

	private static final JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();

	public volatile boolean exit = false;

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Thread#run()
	 */

	public void run() {
		logger.info("DataCacheService run ....");

		try {
			Date now = new Date();
			String dayFlag = DateUtils.format(now,
					DateConstants.FORMAT_YYYY_MM_DD);
			// 设置当天的数据
			result = getDayResult(dayFlag);
			// 当月
			String monthFlag = dayFlag.substring(0, 7);
			// 当年
			String yearFlag = dayFlag.substring(0, 4);
			// 设置当年统计数据
			// setThisYearData(yearFlag, monthFlag);
			Ana1stData ana1stData = govAnalysisService.queryTotalData(null,
					yearFlag);
			if (null != ana1stData) {
				totalNum = ana1stData.getSumNum();
				totalPrice = ana1stData.getSumPrice_();
			}
			else {
				totalNum = new BigDecimal(0);
				totalPrice = new BigDecimal(0);
			}

			// 设置到目前为止所有的统计数据
			setTotalData(yearFlag, monthFlag);
			result.putAll(recentlyData);
		}
		catch (Exception e) {
			e.printStackTrace();
			logger.error("query realtime data occur exception:" + e.toString());
		}

	}

	/**
	 * result
	 * 
	 * @return the result
	 * @since 2015年10月29日 下午3:20:56
	 * @author 徐韶兵
	 * @see #result
	 */
	public JSONObject getResult() {
		return result;
	}

	/**
	 * @return the result
	 * @since 2015年10月29日 下午3:20:56
	 * @author 徐韶兵
	 * @see #result
	 */
	public JSONObject getResult(String dayFlag) {
		if (StringUtils.isBlank(dayFlag)) {
			Date now = new Date();
			dayFlag = DateUtils.format(now, DateConstants.FORMAT_YYYY_MM_DD);
		}
		JSONObject result = new JSONObject();
		result.putAll(getDayResult(dayFlag));
		result.putAll(recentlyData);
		return result;
	}

	/**
	 * @return the result
	 * @since 2015年10月29日 下午3:20:56
	 * @author 徐韶兵
	 * @see #result
	 */
	public JSONObject getDayResult(String dayFlag) {
		String monthFlag = dayFlag.substring(0, 7);
		String yearFlag = dayFlag.substring(0, 4);
		JSONObject result = new JSONObject();
		// 查询当天所有二维数据
		Map<DimensionFlag, List<Ana2ndData>> dayTopNDataMap = govAnalysisService
				.queryDayTopNData(dayFlag, 10, "SUM_NUM",
						DimensionFlag.GOODS_TYPE, DimensionFlag.LOGIS_TYPE,
						DimensionFlag.SHOPS_COMPANY, DimensionFlag.EXPORT_PORT);
		Map<DimensionFlag, List<Ana2ndData>> portYearTopNDataMap = govAnalysisService
				.queryTopNData(null, yearFlag, 10, "SUM_NUM",
						DimensionFlag.EXPORT_PORT);

		Map<DimensionFlag, List<Ana2ndData>> dayCountryTopNDataMap = govAnalysisService
				.queryDayTopNData(dayFlag, 10, "SUM_NUM",
						DimensionFlag.EXPORT_COUNTRY);

		// 累计物流方式数据
		Map<DimensionFlag, List<Ana2ndData>> yearTopNDataLogisMapTotal = govAnalysisService
				.queryTopNData(null, yearFlag, 0, "SUM_NUM",
						DimensionFlag.LOGIS_TYPE, DimensionFlag.LOGIS_COMPANY);
		// 当年物流方式数据
		Map<DimensionFlag, List<Ana2ndData>> yearTopNDataLogisMap = govAnalysisService
				.queryYearTopNData(yearFlag, 0, "SUM_NUM",
						DimensionFlag.LOGIS_TYPE, DimensionFlag.LOGIS_COMPANY);
		// 当日
		Map<DimensionFlag, List<Ana2ndData>> dayTopNDataLogisMap = govAnalysisService
				.queryDayTopNData(dayFlag, 0, "SUM_NUM",
						DimensionFlag.LOGIS_TYPE, DimensionFlag.LOGIS_COMPANY);

		result.put("LogisTypePieDataTotal",
				getLogisPieData(yearTopNDataLogisMapTotal
						.get(DimensionFlag.LOGIS_TYPE)));
		result.put(
				"LogisTypeTableData",
				getLogisTableData(
						yearTopNDataLogisMapTotal.get(DimensionFlag.LOGIS_TYPE),
						yearTopNDataLogisMap.get(DimensionFlag.LOGIS_TYPE),
						dayTopNDataLogisMap.get(DimensionFlag.LOGIS_TYPE)));
		result.put("LogisCompanyPieDataTotal",
				getLogisCompanyPieData(yearTopNDataLogisMapTotal
						.get(DimensionFlag.LOGIS_COMPANY)));
		result.put(
				"LogisCompanyTableData",
				getLogisCompanyTableData(yearTopNDataLogisMapTotal
						.get(DimensionFlag.LOGIS_COMPANY), yearTopNDataLogisMap
						.get(DimensionFlag.LOGIS_COMPANY), dayTopNDataLogisMap
						.get(DimensionFlag.LOGIS_COMPANY)));
		// 国家排名
		result.put("CountryTopNData",
				dayCountryTopNDataMap.get(DimensionFlag.EXPORT_COUNTRY));
		// 电商排名
		result.put("ShopCompanyTopNData",
				dayTopNDataMap.get(DimensionFlag.SHOPS_COMPANY));

		Map<String, List<Ana2ndData>> portData = new HashMap<String, List<Ana2ndData>>();
		portData.put("dayData", dayTopNDataMap.get(DimensionFlag.EXPORT_PORT));
		portData.put("totalData",
				portYearTopNDataMap.get(DimensionFlag.EXPORT_PORT));

		// 口岸排名
		result.put("ExportPortData", getExportData(portData));

		// 出口地图数据
		result.put(
				"WorldExportData",
				getWorldExportData(
						"义乌",
						dayCountryTopNDataMap.get(DimensionFlag.EXPORT_COUNTRY),
						10, true));

		// 商品饼图数据
		result.put("GoodsPieData",
				getGoodsPieData(dayTopNDataMap.get(DimensionFlag.GOODS_TYPE)));
		// 物流方式饼图数据
		result.put("LogisTypePieData", getLogisTypePieData(dayTopNDataMap
				.get(DimensionFlag.LOGIS_TYPE)));

		// 近一周数据查询
		List<Ana1stData> dayRecentlyDataList = govAnalysisService
				.queryDayRecentlyData(
						DateUtils.getSpecifiedDayBefore(dayFlag, 6), dayFlag);
		// 近一个星期出口数据
		result.put("RecentlyDayData",
				getRecentlyDayData(dayFlag, dayRecentlyDataList));
		// 近一周出口金额数据
		result.put("RecentlyDayPriceData",
				getRecentlyDayPriceData(dayFlag, dayRecentlyDataList));

		List<Ana2ndData> gepDayData = govAnalysisService.queryTotalData(
				dayFlag, dayFlag, AnaTable.TB_ANA_DATA_2ND_GOV_D, null,
				DimensionFlag.GEP_LOGIS_CODE);
		List<Ana2ndData> gepMonthData = govAnalysisService.queryTotalData(
				monthFlag, monthFlag, AnaTable.TB_ANA_DATA_2ND_GOV_M, null,
				DimensionFlag.GEP_LOGIS_CODE);
		List<Ana2ndData> gepYearData = govAnalysisService.queryTotalData(
				yearFlag, yearFlag, AnaTable.TB_ANA_DATA_2ND_GOV_Y, null,
				DimensionFlag.GEP_LOGIS_CODE);
		result.put("TotalGepData",
				getTotalGepData(gepDayData, gepMonthData, gepYearData));
		// 当年数据查询
		Ana1stData yearData = govAnalysisService.queryYearData(yearFlag);
		// 当月数据查询
		Ana1stData monthData = govAnalysisService.queryMonthData(monthFlag);
		// 当天数据查询
		Ana1stData dayData = govAnalysisService.queryDayData(dayFlag);
		// 总统计数据
		result.put("ThisTotalData",
				getThisTotalData(yearData, monthData, dayData, dayFlag));
		return result;
	}

	private JSONObject getTotalGepData(List<Ana2ndData> gepDayData,
			List<Ana2ndData> gepMonthData, List<Ana2ndData> gepYearData) {
		JSONObject jsonObject = new JSONObject();
		Map<String, ConfParam> allGepLogisMap = cdc.getParamsMap().get(
				ParamFlag.GEP_LOGIS_TYPE.getFlag());
		jsonObject.put("logisticsNum", allGepLogisMap.size());
		jsonObject.put("warehouseNum", allGepLogisMap.size());
		BigDecimal dayNum = new BigDecimal(0);
		BigDecimal dayAmount = new BigDecimal(0);
		BigDecimal monthNum = new BigDecimal(0);
		BigDecimal monthAmount = new BigDecimal(0);
		BigDecimal yearNum = new BigDecimal(0);
		BigDecimal yearAmount = new BigDecimal(0);
		for (Ana2ndData day : gepDayData) {

			dayNum = dayNum.add(day.getSumNum());
			dayAmount = dayAmount.add(day.getSumPrice());
		}
		for (Ana2ndData month : gepMonthData) {

			monthNum = monthNum.add(month.getSumNum());
			monthAmount = monthAmount.add(month.getSumPrice());
		}
		for (Ana2ndData year : gepYearData) {

			yearNum = yearNum.add(year.getSumNum());
			yearAmount = yearAmount.add(year.getSumPrice());
		}
		jsonObject.put("dayNum", dayNum);
		jsonObject.put("dayAmount", dayAmount);
		jsonObject.put("monthNum", monthNum);
		jsonObject.put("monthAmount", monthAmount);
		jsonObject.put("yearNum", yearNum);
		jsonObject.put("yearAmount", yearAmount);
		return jsonObject;
	}

	private JSONArray getLogisTableData(List<Ana2ndData> total,
			List<Ana2ndData> year, List<Ana2ndData> day) {
		Map<String, ConfParam> allLogisMap = cdc.getParamsMap().get(
				ParamFlag.LOGIS_TYPE.getFlag());
		JSONArray result = new JSONArray();
		if (null == total) {
			total = new ArrayList<Ana2ndData>();
		}
		if (null == year) {
			year = new ArrayList<Ana2ndData>();
		}
		if (null == day) {
			day = new ArrayList<Ana2ndData>();
		}
		Map<String, String> dayMap = new HashMap<String, String>();
		Map<String, String> yearMap = new HashMap<String, String>();
		Map<String, String> totalMap = new HashMap<String, String>();
		for (Ana2ndData data : day) {
			dayMap.put(data.getMainCode(), data.getSumNum().toString());
		}
		for (Ana2ndData data : year) {
			yearMap.put(data.getMainCode(), data.getSumNum().toString());
		}
		for (Ana2ndData data : total) {
			totalMap.put(data.getMainCode(), data.getSumNum().toString());
		}
		Iterator<Entry<String, ConfParam>> iterator0 = allLogisMap.entrySet()
				.iterator();
		while (iterator0.hasNext()) {
			Entry<String, ConfParam> entry = iterator0.next();
			if (!dayMap.containsKey(entry.getKey())) {
				dayMap.put(entry.getKey(), "0");
			}
			if (!yearMap.containsKey(entry.getKey())) {
				yearMap.put(entry.getKey(), "0");
			}
			if (!totalMap.containsKey(entry.getKey())) {
				totalMap.put(entry.getKey(), "0");
			}
		}
		Iterator<Entry<String, ConfParam>> iterator = allLogisMap.entrySet()
				.iterator();
		List<LogisTypeDataVo> vos = new ArrayList<LogisTypeDataVo>();
		while (iterator.hasNext()) {
			Entry<String, ConfParam> entry = iterator.next();
			LogisTypeDataVo vo = new LogisTypeDataVo();
			vo.setLogisTypeCode(entry.getKey());
			vo.setLogisTypeName(entry.getValue().getName());
			vo.setDayData(dayMap.get(entry.getKey()));
			vo.setYearData(yearMap.get(entry.getKey()));
			vo.setTotalData(totalMap.get(entry.getKey()));
			vos.add(vo);
		}
		Collections.sort(vos);
		List<LogisTypeDataVo> finalResult = new ArrayList<LogisTypeDataVo>();
		if (vos.size() > 7) {
			long dayNum = 0L, yearNum = 0L, totalNum = 0L;
			for (int i = 0; i < vos.size(); i++) {
				LogisTypeDataVo vo = vos.get(i);

				if (i < 6) {
					finalResult.add(vo);
				}
				else {
					dayNum += Long.parseLong(vo.getDayData());
					yearNum += Long.parseLong(vo.getYearData());
					totalNum += Long.parseLong(vo.getTotalData());
				}
			}
			LogisTypeDataVo other = new LogisTypeDataVo();
			other.setLogisTypeName("其他");
			other.setTotalData(String.valueOf(totalNum));
			other.setYearData(String.valueOf(yearNum));
			other.setDayData(String.valueOf(dayNum));
			finalResult.add(other);
		}
		else {
			finalResult = vos;
		}
		Collections.sort(finalResult);
		result.addAll(finalResult);
		return result;
	}

	private JSONArray getLogisPieData(List<Ana2ndData> total) {
		Map<String, ConfParam> allLogisMap = cdc.getParamsMap().get(
				ParamFlag.LOGIS_TYPE.getFlag());
		JSONArray result = new JSONArray();
		if (null == total) {
			total = new ArrayList<Ana2ndData>();
		}

		Map<String, String> totalMap = new HashMap<String, String>();

		for (Ana2ndData data : total) {
			totalMap.put(data.getMainCode(), data.getSumNum().toString());
		}
		Iterator<Entry<String, ConfParam>> iterator0 = allLogisMap.entrySet()
				.iterator();
		while (iterator0.hasNext()) {
			Entry<String, ConfParam> entry = iterator0.next();

			if (!totalMap.containsKey(entry.getKey())) {
				totalMap.put(entry.getKey(), "0");
			}
		}
		Iterator<Entry<String, ConfParam>> iterator = allLogisMap.entrySet()
				.iterator();
		List<LogisTypeDataVo> vos = new ArrayList<LogisTypeDataVo>();
		while (iterator.hasNext()) {
			Entry<String, ConfParam> entry = iterator.next();
			LogisTypeDataVo vo = new LogisTypeDataVo();
			vo.setLogisTypeCode(entry.getKey());
			vo.setLogisTypeName(entry.getValue().getName());

			vo.setTotalData(totalMap.get(entry.getKey()));
			vos.add(vo);
		}
		Collections.sort(vos);
		List<LogisTypeDataVo> finalResult = new ArrayList<LogisTypeDataVo>();
		if (vos.size() > 7) {
			long totalNum = 0L;
			for (int i = 0; i < vos.size(); i++) {
				LogisTypeDataVo vo = vos.get(i);

				if (i < 6) {
					finalResult.add(vo);
				}
				else {

					totalNum += Long.parseLong(vo.getTotalData());
				}
			}
			LogisTypeDataVo other = new LogisTypeDataVo();
			other.setLogisTypeName("其他");
			other.setTotalData(String.valueOf(totalNum));

			finalResult.add(other);
		}
		else {
			finalResult = vos;
		}

		for (int i = 0; i < finalResult.size(); i++) {
			LogisTypeDataVo ana2ndData = finalResult.get(i);
			JSONObject nameValue = new JSONObject();
			nameValue.put("name", ana2ndData.getLogisTypeName());
			nameValue.put("value", ana2ndData.getTotalData());
			result.add(nameValue);
		}

		return result;
	}

	private JSONArray getLogisCompanyTableData(List<Ana2ndData> total,
			List<Ana2ndData> year, List<Ana2ndData> day) {
		Map<String, ConfParam> allLogisMap = cdc.getParamsMap().get(
				ParamFlag.LOGIS_COMPANY.getFlag());
		JSONArray result = new JSONArray();
		if (null == total) {
			total = new ArrayList<Ana2ndData>();
		}
		if (null == year) {
			year = new ArrayList<Ana2ndData>();
		}
		if (null == day) {
			day = new ArrayList<Ana2ndData>();
		}
		Map<String, String> dayMap = new HashMap<String, String>();
		Map<String, String> yearMap = new HashMap<String, String>();
		Map<String, String> totalMap = new HashMap<String, String>();
		for (Ana2ndData data : day) {
			dayMap.put(data.getMainCode(), data.getSumNum().toString());
		}
		for (Ana2ndData data : year) {
			yearMap.put(data.getMainCode(), data.getSumNum().toString());
		}
		for (Ana2ndData data : total) {
			totalMap.put(data.getMainCode(), data.getSumNum().toString());
		}
		Iterator<Entry<String, ConfParam>> iterator0 = allLogisMap.entrySet()
				.iterator();
		while (iterator0.hasNext()) {
			Entry<String, ConfParam> entry = iterator0.next();
			if (!dayMap.containsKey(entry.getKey())) {
				dayMap.put(entry.getKey(), "0");
			}
			if (!yearMap.containsKey(entry.getKey())) {
				yearMap.put(entry.getKey(), "0");
			}
			if (!totalMap.containsKey(entry.getKey())) {
				totalMap.put(entry.getKey(), "0");
			}
		}
		Iterator<Entry<String, ConfParam>> iterator = allLogisMap.entrySet()
				.iterator();
		List<LogisTypeDataVo> vos = new ArrayList<LogisTypeDataVo>();
		while (iterator.hasNext()) {
			Entry<String, ConfParam> entry = iterator.next();
			LogisTypeDataVo vo = new LogisTypeDataVo();
			vo.setLogisTypeCode(entry.getKey());
			vo.setLogisTypeName(entry.getValue().getName());
			vo.setDayData(dayMap.get(entry.getKey()));
			vo.setYearData(yearMap.get(entry.getKey()));
			vo.setTotalData(totalMap.get(entry.getKey()));
			vos.add(vo);
		}
		Collections.sort(vos);
		List<LogisTypeDataVo> finalResult = new ArrayList<LogisTypeDataVo>();
		if (vos.size() > 4) {
			long dayNum = 0L, yearNum = 0L, totalNum = 0L;
			for (int i = 0; i < vos.size(); i++) {
				LogisTypeDataVo vo = vos.get(i);

				if (i < 3) {
					finalResult.add(vo);
				}
				else {
					dayNum += Long.parseLong(vo.getDayData());
					yearNum += Long.parseLong(vo.getYearData());
					totalNum += Long.parseLong(vo.getTotalData());
				}
			}
			LogisTypeDataVo other = new LogisTypeDataVo();
			other.setLogisTypeName("其他");
			other.setTotalData(String.valueOf(totalNum));
			other.setYearData(String.valueOf(yearNum));
			other.setDayData(String.valueOf(dayNum));
			finalResult.add(other);
		}
		else {
			finalResult = vos;
		}
		Collections.sort(finalResult);
		result.addAll(finalResult);
		return result;
	}

	private JSONArray getLogisCompanyPieData(List<Ana2ndData> total) {
		Map<String, ConfParam> allLogisMap = cdc.getParamsMap().get(
				ParamFlag.LOGIS_COMPANY.getFlag());
		JSONArray result = new JSONArray();
		if (null == total) {
			total = new ArrayList<Ana2ndData>();
		}

		Map<String, String> totalMap = new HashMap<String, String>();

		for (Ana2ndData data : total) {
			totalMap.put(data.getMainCode(), data.getSumNum().toString());
		}
		Iterator<Entry<String, ConfParam>> iterator0 = allLogisMap.entrySet()
				.iterator();
		while (iterator0.hasNext()) {
			Entry<String, ConfParam> entry = iterator0.next();

			if (!totalMap.containsKey(entry.getKey())) {
				totalMap.put(entry.getKey(), "0");
			}
		}
		Iterator<Entry<String, ConfParam>> iterator = allLogisMap.entrySet()
				.iterator();
		List<LogisTypeDataVo> vos = new ArrayList<LogisTypeDataVo>();
		while (iterator.hasNext()) {
			Entry<String, ConfParam> entry = iterator.next();
			LogisTypeDataVo vo = new LogisTypeDataVo();
			vo.setLogisTypeCode(entry.getKey());
			vo.setLogisTypeName(entry.getValue().getName());

			vo.setTotalData(totalMap.get(entry.getKey()));
			vos.add(vo);
		}
		Collections.sort(vos);
		List<LogisTypeDataVo> finalResult = new ArrayList<LogisTypeDataVo>();
		if (vos.size() > 4) {
			long totalNum = 0L;
			for (int i = 0; i < vos.size(); i++) {
				LogisTypeDataVo vo = vos.get(i);

				if (i < 3) {
					finalResult.add(vo);
				}
				else {

					totalNum += Long.parseLong(vo.getTotalData());
				}
			}
			LogisTypeDataVo other = new LogisTypeDataVo();
			other.setLogisTypeName("其他");
			other.setTotalData(String.valueOf(totalNum));

			finalResult.add(other);
		}
		else {
			finalResult = vos;
		}

		for (int i = 0; i < finalResult.size(); i++) {
			LogisTypeDataVo ana2ndData = finalResult.get(i);
			JSONObject nameValue = new JSONObject();
			nameValue.put("name", ana2ndData.getLogisTypeName());
			nameValue.put("value", ana2ndData.getTotalData());
			result.add(nameValue);
		}

		return result;
	}

	private JSONArray getExportData(Map<String, List<Ana2ndData>> portData) {
		JSONArray result = new JSONArray();
		List<Ana2ndData> dayData = portData.get("dayData");
		//
		List<Ana2ndData> totalData = portData.get("totalData");
		if (null == dayData) {
			dayData = new ArrayList<Ana2ndData>();
		}
		if (null == totalData) {
			totalData = new ArrayList<Ana2ndData>();
		}
		Map<String, String> portName = new HashMap<String, String>();
		portName.put("义乌", "");
		portName.put("杭州", "");
		portName.put("深圳", "");
		portName.put("上海", "");
		portName.put("北京", "");
		portName.put("阿拉山口", "");
		Map<String, String> dayMap = new HashMap<String, String>();
		Map<String, String> totalMap = new HashMap<String, String>();
		for (Ana2ndData data : dayData) {
			dayMap.put(data.getMainName(), data.getSumNum().toString());
		}
		for (Ana2ndData data : totalData) {
			totalMap.put(data.getMainName(), data.getSumNum().toString());
		}

		// 以totaldata为准
		Iterator<Entry<String, String>> iterator0 = portName.entrySet()
				.iterator();
		while (iterator0.hasNext()) {
			Entry<String, String> entry = iterator0.next();
			if (!dayMap.containsKey(entry.getKey())) {
				dayMap.put(entry.getKey(), "0");
			}
			if (!totalMap.containsKey(entry.getKey())) {
				totalMap.put(entry.getKey(), "0");
			}
		}

		Iterator<Entry<String, String>> iterator = portName.entrySet()
				.iterator();
		List<ExportPortDataVo> vos = new ArrayList<ExportPortDataVo>();
		while (iterator.hasNext()) {
			Entry<String, String> entry = iterator.next();
			ExportPortDataVo vo = new ExportPortDataVo();
			vo.setPortCity(entry.getKey());
			vo.setDayData(dayMap.get(entry.getKey()));
			vo.setTotalData(totalMap.get(entry.getKey()));
			vos.add(vo);
		}
		Collections.sort(vos);
		result.addAll(vos);

		return result;
	}

	/**
	 * 组装近半年柱状图数据 <br>
	 * (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param monthRecentlyDataList
	 *            近半年统计数据
	 * @return 近半年柱状图数据
	 * @exception
	 * @since 2015年10月29日 下午6:56:18
	 * @author 徐韶兵
	 */
	private JSONObject getRecentlyMonthData(String monthFlag,
			List<Ana1stData> monthRecentlyDataList, int months) {
		JSONObject result = new JSONObject();
		JSONArray nameArray = new JSONArray();
		JSONArray countArray = new JSONArray();
		String thisYear = monthFlag.substring(0, 4);
		int thisYearIndex = 0;
		// 近一年当中肯定有一月份
		String thisYearJan = thisYear + "-01";
		// 获取日期列表
		List<String> dateList2 = DateUtils
				.getDateList(
						DateUtils.getSpecifiedMonthBefore(monthFlag, months),
						monthFlag);
		thisYearIndex = dateList2.indexOf(thisYearJan);
		Map<String, Ana1stData> dateMap2 = new HashMap<String, Ana1stData>();
		if (monthRecentlyDataList == null) {
			monthRecentlyDataList = new ArrayList<Ana1stData>();
		}
		else {
			for (Ana1stData tdStatisticsTotal : monthRecentlyDataList) {
				dateMap2.put(tdStatisticsTotal.getDateFlag(), tdStatisticsTotal);
			}
			monthRecentlyDataList = new ArrayList<Ana1stData>();
		}
		for (String date : dateList2) {
			Ana1stData tdStatisticsTotal = dateMap2.get(date);
			if (tdStatisticsTotal != null) {
				monthRecentlyDataList.add(tdStatisticsTotal);
			}
			else {
				tdStatisticsTotal = new Ana1stData();
				tdStatisticsTotal.setDateFlag(date);
				tdStatisticsTotal.setSumNum(new BigDecimal(0));
				monthRecentlyDataList.add(tdStatisticsTotal);
			}
		}

		for (int i = 0; i < monthRecentlyDataList.size(); i++) {

			Ana1stData ana1stData = null;
			try {
				ana1stData = monthRecentlyDataList.get(i);
				String dateFlag = ana1stData.getDateFlag();

				if (i == 0 || i == thisYearIndex) {

					nameArray.add(Integer.parseInt(dateFlag.substring(0, 4))
							+ "年" + Integer.parseInt(dateFlag.substring(5, 7))
							+ "月");

				}
				else {

					nameArray.add(Integer.parseInt(dateFlag.substring(5, 7))
							+ "月");
				}
				countArray.add(ana1stData.getSumNum_());
			}
			catch (Exception e) {
				logger.error(e.getMessage());
			}
		}
		result.put("nameArray", nameArray);
		result.put("countArray", countArray);
		return result;
	}

	private JSONObject getRecentlyMonthPriceData(String monthFlag,
			List<Ana1stData> monthRecentlyDataList, int months) {
		JSONObject result = new JSONObject();
		JSONArray nameArray = new JSONArray();
		JSONArray countArray = new JSONArray();

		// 获取日期列表
		List<String> dateList2 = DateUtils
				.getDateList(
						DateUtils.getSpecifiedMonthBefore(monthFlag, months),
						monthFlag);

		Map<String, Ana1stData> dateMap2 = new HashMap<String, Ana1stData>();
		if (monthRecentlyDataList == null) {
			monthRecentlyDataList = new ArrayList<Ana1stData>();
		}
		else {
			for (Ana1stData tdStatisticsTotal : monthRecentlyDataList) {
				dateMap2.put(tdStatisticsTotal.getDateFlag(), tdStatisticsTotal);
			}
			monthRecentlyDataList = new ArrayList<Ana1stData>();
		}
		for (String date : dateList2) {
			Ana1stData tdStatisticsTotal = dateMap2.get(date);
			if (tdStatisticsTotal != null) {
				monthRecentlyDataList.add(tdStatisticsTotal);
			}
			else {
				tdStatisticsTotal = new Ana1stData();
				tdStatisticsTotal.setDateFlag(date);
				tdStatisticsTotal.setSumNum(new BigDecimal(0));
				tdStatisticsTotal.setSumPrice(new BigDecimal(0));
				monthRecentlyDataList.add(tdStatisticsTotal);
			}
		}

		for (int i = 0; i < monthRecentlyDataList.size(); i++) {

			Ana1stData ana1stData = null;
			try {
				ana1stData = monthRecentlyDataList.get(i);
				String dateFlag = ana1stData.getDateFlag();

				nameArray.add(Integer.parseInt(dateFlag.substring(0, 4)) + "年"
						+ Integer.parseInt(dateFlag.substring(5, 7)) + "月");

				countArray.add(ana1stData.getSumPrice_());
			}
			catch (Exception e) {
				logger.error(e.getMessage());
			}
		}
		result.put("nameArray", nameArray);
		result.put("countArray", countArray);
		return result;
	}

	private JSONObject getRecently12Data(String monthFlag,
			List<Ana2ndData> monthRecentlyDataList, int months, boolean flag) {
		JSONObject result = new JSONObject();
		JSONArray nameArray = new JSONArray();
		JSONArray countArray = new JSONArray();
		JSONArray priceArray = new JSONArray();

		// 获取日期列表
		List<String> dateList2 = DateUtils
				.getDateList(
						DateUtils.getSpecifiedMonthBefore(monthFlag, months),
						monthFlag);

		Map<String, Ana2ndData> dateMap2 = new HashMap<String, Ana2ndData>();
		if (monthRecentlyDataList == null) {
			monthRecentlyDataList = new ArrayList<Ana2ndData>();
		}
		else {
			for (Ana2ndData tdStatisticsTotal : monthRecentlyDataList) {
				dateMap2.put(tdStatisticsTotal.getDateFlag(), tdStatisticsTotal);
			}
			monthRecentlyDataList = new ArrayList<Ana2ndData>();
		}
		for (String date : dateList2) {
			Ana2ndData tdStatisticsTotal = dateMap2.get(date);
			if (tdStatisticsTotal != null) {
				monthRecentlyDataList.add(tdStatisticsTotal);
			}
			else {
				tdStatisticsTotal = new Ana2ndData();
				tdStatisticsTotal.setDateFlag(date);
				tdStatisticsTotal.setSumNum(new BigDecimal(0));
				tdStatisticsTotal.setSumPrice(new BigDecimal(0));
				monthRecentlyDataList.add(tdStatisticsTotal);
			}
		}

		for (int i = 0; i < monthRecentlyDataList.size(); i++) {

			Ana2ndData ana2ndData = null;
			try {
				ana2ndData = monthRecentlyDataList.get(i);
				String dateFlag = ana2ndData.getDateFlag();

				nameArray.add(Integer.parseInt(dateFlag.substring(5, 7)) + "月");
				if (flag) {
					// 万票万美元
					priceArray.add(ana2ndData.getSumPrice_());
					countArray.add(ana2ndData.getSumNum_());
				}
				else {
					priceArray.add(ana2ndData.getSumPrice());
					countArray.add(ana2ndData.getSumNum());
				}

			}
			catch (Exception e) {
				logger.error(e.getMessage());
			}
		}
		result.put("nameArray", nameArray);
		result.put("countArray", countArray);
		result.put("priceArray", priceArray);
		return result;
	}

	private JSONObject getRecently12GepData(String monthFlag,
			List<Ana2ndData> monthRecentlyDataList, int months, boolean flag) {
		JSONObject result = new JSONObject();
		JSONArray nameArray = new JSONArray();
		JSONArray legendArray = new JSONArray();

		Map<String, ConfParam> allGepLogisMap = cdc.getParamsMap().get(
				ParamFlag.GEP_LOGIS_TYPE.getFlag());

		// 获取日期列表
		List<String> dateList2 = DateUtils.getDateList(
				DateUtils.getSpecifiedMonthBefore(monthFlag, months),

				monthFlag);
		// 每个月都有一个Map<String,Ana2ndData>
		Map<String, Map<String, Ana2ndData>> data = new TreeMap<String, Map<String, Ana2ndData>>();

		for (String date : dateList2) {
			if (!data.containsKey(date)) {

				data.put(date, new HashMap<String, Ana2ndData>());
			}
		}

		for (Ana2ndData tdStatisticsTotal : monthRecentlyDataList) {
			data.get(tdStatisticsTotal.getDateFlag()).put(
					tdStatisticsTotal.getMainCode(), tdStatisticsTotal);

		}

		Iterator<Entry<String, Map<String, Ana2ndData>>> iterator0 = data
				.entrySet().iterator();
		while (iterator0.hasNext()) {
			Entry<String, Map<String, Ana2ndData>> entry = iterator0.next();
			String key = entry.getKey();
			Map<String, Ana2ndData> valueMap = entry.getValue();

			Iterator<Entry<String, ConfParam>> iterator1 = allGepLogisMap
					.entrySet().iterator();
			while (iterator1.hasNext()) {
				Entry<String, ConfParam> entry1 = iterator1.next();

				if (!valueMap.containsKey(entry1.getKey())) {
					Ana2ndData ana2ndData = new Ana2ndData();
					ana2ndData.setDateFlag(key);
					ana2ndData.setSumNum(new BigDecimal(0));
					ana2ndData.setSumPrice(new BigDecimal(0));
					valueMap.put(entry1.getKey(), ana2ndData);
				}
			}

		}
		Iterator<Entry<String, Map<String, Ana2ndData>>> iterator1 = data
				.entrySet().iterator();
		Map<String, List<String>> viewData = new HashMap<String, List<String>>();
		Iterator<Entry<String, ConfParam>> iteratorLogis = allGepLogisMap
				.entrySet().iterator();
		while (iteratorLogis.hasNext()) {
			Entry<String, ConfParam> entryLogis = iteratorLogis.next();

			List<String> list = new ArrayList<String>();
			viewData.put(entryLogis.getKey(), list);
			legendArray.add(allGepLogisMap.get(entryLogis.getKey()).getName());
		}
		while (iterator1.hasNext()) {
			Entry<String, Map<String, Ana2ndData>> entry = iterator1.next();
			String key = entry.getKey();
			Map<String, Ana2ndData> valueMap = entry.getValue();
			nameArray.add(Integer.parseInt(key.substring(0, 4)) + "年"
					+ Integer.parseInt(key.substring(5, 7)) + "月");
			Iterator<Entry<String, Ana2ndData>> iterator2 = valueMap.entrySet()
					.iterator();
			while (iterator2.hasNext()) {
				Entry<String, Ana2ndData> entry1 = iterator2.next();
				String logisticsId = entry1.getKey();
				viewData.get(logisticsId).add(
						entry1.getValue().getSumNum().toString());

			}

		}
		Iterator<Entry<String, List<String>>> viewIterator = viewData
				.entrySet().iterator();
		JSONArray array = new JSONArray();
		while (viewIterator.hasNext()) {
			Entry<String, List<String>> entry = viewIterator.next();
			JSONObject object = new JSONObject();
			object.put("name", allGepLogisMap.get(entry.getKey()).getName());
			object.put("type", "line");
			object.put("smooth", true);
			JSONObject obj0 = new JSONObject();
			JSONObject obj1 = new JSONObject();
			JSONObject obj2 = new JSONObject();
			obj2.put("type", "default");
			obj1.put("areaStyle", obj2);
			obj0.put("normal", obj1);
			object.put("itemStyle", obj0);
			object.put("data", entry.getValue());
			array.add(object);
		}

		result.put("nameArray", nameArray);
		result.put("series", array);
		result.put("logisArray", legendArray);

		return result;
	}

	/**
	 * 组装近7天数据柱状图数据 <br>
	 * (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param dayRecentlyDataList
	 *            近7天数据
	 * @return 近7天数据柱状图数据
	 * @exception
	 * @since 2015年10月29日 下午6:55:29
	 * @author 徐韶兵
	 */
	private JSONObject getRecentlyDayData(String dayFlag,
			List<Ana1stData> dayRecentlyDataList) {
		JSONObject result = new JSONObject();
		JSONArray nameArray = new JSONArray();
		JSONArray countArray = new JSONArray();
		// 获取日期列表
		List<String> dateList = DateUtils.getDateList(
				DateUtils.getSpecifiedDayBefore(dayFlag, 6), dayFlag);
		Map<String, Ana1stData> dateMap = new HashMap<String, Ana1stData>();
		if (dayRecentlyDataList == null) {
			dayRecentlyDataList = new ArrayList<Ana1stData>();
		}
		else {
			for (Ana1stData tdStatisticsTotal : dayRecentlyDataList) {
				dateMap.put(tdStatisticsTotal.getDateFlag(), tdStatisticsTotal);
			}
			dayRecentlyDataList = new ArrayList<Ana1stData>();
		}
		for (String date : dateList) {
			Ana1stData tdStatisticsTotal = dateMap.get(date);
			if (tdStatisticsTotal != null) {
				dayRecentlyDataList.add(tdStatisticsTotal);
			}
			else {
				tdStatisticsTotal = new Ana1stData();
				tdStatisticsTotal.setDateFlag(date);
				tdStatisticsTotal.setSumNum(new BigDecimal(0));
				dayRecentlyDataList.add(tdStatisticsTotal);
			}
		}
		for (int i = 0; i < dayRecentlyDataList.size(); i++) {
			Ana1stData ana1stData = null;
			try {
				ana1stData = dayRecentlyDataList.get(i);
				nameArray.add(ana1stData.getDateFlag().substring(5, 10));
				countArray.add(ana1stData.getSumNum());
			}
			catch (NullPointerException e) {
				logger.error(e.getMessage());
			}
		}
		result.put("nameArray", nameArray);
		result.put("countArray", countArray);
		return result;
	}

	private JSONObject getRecentlyDayPriceData(String dayFlag,
			List<Ana1stData> dayRecentlyDataList) {
		JSONObject result = new JSONObject();
		JSONArray nameArray = new JSONArray();
		JSONArray countArray = new JSONArray();
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM月dd日");
		SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
		// 获取日期列表
		List<String> dateList = DateUtils.getDateList(
				DateUtils.getSpecifiedDayBefore(dayFlag, 6), dayFlag);
		Map<String, Ana1stData> dateMap = new HashMap<String, Ana1stData>();
		if (dayRecentlyDataList == null) {
			dayRecentlyDataList = new ArrayList<Ana1stData>();
		}
		else {
			for (Ana1stData tdStatisticsTotal : dayRecentlyDataList) {
				dateMap.put(tdStatisticsTotal.getDateFlag(), tdStatisticsTotal);
			}
			dayRecentlyDataList = new ArrayList<Ana1stData>();
		}
		for (String date : dateList) {
			Ana1stData tdStatisticsTotal = dateMap.get(date);
			if (tdStatisticsTotal != null) {
				dayRecentlyDataList.add(tdStatisticsTotal);
			}
			else {
				tdStatisticsTotal = new Ana1stData();
				tdStatisticsTotal.setDateFlag(date);
				tdStatisticsTotal.setSumPrice(new BigDecimal(0));
				dayRecentlyDataList.add(tdStatisticsTotal);
			}
		}
		for (int i = 0; i < dayRecentlyDataList.size(); i++) {
			Ana1stData ana1stData = null;
			try {
				ana1stData = dayRecentlyDataList.get(i);
				String day = ana1stData.getDateFlag().substring(0, 10);
				Date date = null;
				try {
					date = dateFormat1.parse(day);
				}
				catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (null != date) {
					nameArray.add(dateFormat.format(date));
				}

				countArray.add(ana1stData.getSumPrice());
			}
			catch (NullPointerException e) {
				logger.error(e.getMessage());
			}
		}
		result.put("nameArray", nameArray);
		result.put("countArray", countArray);
		return result;
	}

	/**
	 * 组装商品品类饼图数据 <br>
	 * (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param totalPkg
	 *            总商品件数
	 * @param goodsDataList
	 *            各个商品统计数据信息
	 * @return 商品品类饼图数据
	 * @exception
	 * @since 2015年10月29日 下午6:54:13
	 * @author 徐韶兵
	 */
	private JSONArray getGoodsPieData(List<Ana2ndData> goodsDataList) {

		JSONArray result = new JSONArray();
		if (goodsDataList != null) {
			BigDecimal persent = new BigDecimal(100);
			BigDecimal total = new BigDecimal(0);
			for (int i = 0; i < goodsDataList.size(); i++) {
				Ana2ndData ana2ndData = goodsDataList.get(i);
				JSONObject nameValue = new JSONObject();
				nameValue.put("name", ana2ndData.getMainName());
				nameValue.put("value", ana2ndData.getSumNum());
				result.add(nameValue);
				total = total.add(ana2ndData.getSumNum());
			}
			for (int i = 0; i < result.size(); i++) {
				JSONObject nameValue = result.getJSONObject(i);
				String name = nameValue.getString("name");
				BigDecimal value = nameValue.getBigDecimal("value");
				value = value.multiply(persent);
				value = value.divide(total, 2, BigDecimal.ROUND_HALF_UP);
				name = name + " " + value + "%";
				nameValue.put("name", name);
			}
		}

		return result;
	}

	private JSONArray getMCategoryData(List<Ana2ndData> mCategoryDataList) {

		JSONArray result = new JSONArray();
		Map<String, MCategory> mMap = cdc.getmCategoryMap();

		if (mCategoryDataList != null) {

			for (int i = 0; i < mCategoryDataList.size(); i++) {
				Ana2ndData ana2ndData = mCategoryDataList.get(i);
				JSONObject nameValue = new JSONObject();
				MCategory mCategory = mMap.get(ana2ndData.getMainCode());
				String categoryName = "其他";

				if (null != mCategory) {
					categoryName = mCategory.getmCategoryName();
				}

				nameValue.put("name", categoryName);
				nameValue.put("value", ana2ndData.getSumPrice_());
				result.add(nameValue);

			}

		}

		return result;
	}

	private JSONArray getSCategoryData(List<Ana2ndData> sCategoryDataList) {

		JSONArray result = new JSONArray();
		Map<String, SCategory> sMap = cdc.getsCategoryMap();

		if (sCategoryDataList != null) {

			for (int i = 0; i < sCategoryDataList.size(); i++) {
				Ana2ndData ana2ndData = sCategoryDataList.get(i);
				JSONObject nameValue = new JSONObject();
				SCategory sCategory = sMap.get(ana2ndData.getMainCode());
				String categoryName = "其他";

				if (null != sCategory) {
					categoryName = sCategory.getsCategoryName();
				}
				nameValue.put("name", categoryName);
				nameValue.put("value", ana2ndData.getSumPrice_());
				result.add(nameValue);

			}

		}

		return result;
	}

	/**
	 * 组装物流类型饼图数据 <br>
	 * (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param logisTypeDataList
	 *            物流类型数据
	 * @return 物流类型饼图数据
	 * @exception
	 * @since 2015年10月29日 下午6:53:09
	 * @author 徐韶兵
	 */
	private JSONArray getLogisTypePieData(List<Ana2ndData> logisTypeDataList) {
		JSONArray result = new JSONArray();
		if (logisTypeDataList != null) {
			for (int i = 0; i < logisTypeDataList.size(); i++) {
				Ana2ndData ana2ndData = logisTypeDataList.get(i);
				JSONObject nameValue = new JSONObject();
				nameValue.put(
						"name",
						cdc.getName(ParamFlag.LOGIS_TYPE,
								ana2ndData.getMainCode()));
				nameValue.put("value", ana2ndData.getSumNum());
				result.add(nameValue);
			}
		}
		return result;
	}

	/**
	 * 组装出口世界地图展示数据 <br>
	 * (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param exportCity
	 *            出口城市
	 * @param countryDataList
	 *            出口国家列表
	 * @return 出口世界地图展示数据
	 * @exception
	 * @since 2015年10月29日 下午6:52:10
	 * @author 徐韶兵
	 */
	private JSONObject getWorldExportData(String exportCity,
			List<Ana2ndData> countryDataList, int topN, boolean flag) {
		JSONObject result = new JSONObject();
		if (countryDataList != null && countryDataList.size() > 0) {
			if (flag) {
				result.put("max", countryDataList.get(0).getSumNum_());
				result.put("min",
						countryDataList.get(countryDataList.size() - 1)
								.getSumNum_());
			}
			else {
				result.put("max", countryDataList.get(0).getSumNum());
				result.put("min",
						countryDataList.get(countryDataList.size() - 1)
								.getSumNum());
			}

			JSONObject geoCoord = new JSONObject();

			JSONArray topCityArray = new JSONArray();
			topCityArray.add(exportCity);
			JSONObject topCitySelected = new JSONObject();
			topCitySelected.put(exportCity, true);
			JSONArray topCityDataArray = new JSONArray();
			JSONObject topCityData = new JSONObject();
			topCityData.put("cityName", exportCity);

			JSONArray markLineDataArr = new JSONArray();
			JSONArray markPointDataArr = new JSONArray();
			if (topN > countryDataList.size()) {
				topN = countryDataList.size();
			}
			for (int i = 0; i < topN; i++) {
				Ana2ndData ana2ndData = countryDataList.get(i);
				String countryCode = ana2ndData.getMainCode();
				ConfCountry confCountry = cdc.getCountry(countryCode);
				if (null == confCountry) {
					continue;
				}
				Coordinate coordinate = cdc.getCoordinate(
						confCountry.getName(), confCountry.getMarkCity());
				if (null == coordinate) {
					continue;
				}
				// 纬度和经度（latitude and longitude）
				// 如{'Islands':[113.95, 22.26]}，香港离岛区名称显示定位到东经113.95，北纬22.26上
				JSONArray xyAxis = new JSONArray();
				xyAxis.add(Float.parseFloat(coordinate.getLongitude()));
				xyAxis.add(Float.parseFloat(coordinate.getLatitude()));
				geoCoord.put(confCountry.getName(), xyAxis);

				JSONArray markLineData = new JSONArray();
				JSONObject markPointData = new JSONObject();

				markPointData.put("name", confCountry.getName());
				if (flag) {
					markPointData.put("value", ana2ndData.getSumNum_());
				}
				else {
					markPointData.put("value", ana2ndData.getSumNum());
				}

				JSONObject markLine1 = new JSONObject();
				markLine1.put("name", exportCity);

				JSONObject markLine2 = new JSONObject();
				markLine2.put("name", confCountry.getName());
				if (flag) {
					markLine2.put("value", ana2ndData.getSumNum_());
				}
				else {
					markLine2.put("value", ana2ndData.getSumNum());
				}

				markLineData.add(markLine1);
				markLineData.add(markLine2);

				markLineDataArr.add(markLineData);
				markPointDataArr.add(markPointData);
			}

			topCityData.put("markPointDataArr", markPointDataArr);
			topCityData.put("markLineDataArr", markLineDataArr);

			Coordinate coordinate = cdc.getCoordinate("中国", exportCity);
			JSONArray xyAxis = new JSONArray();
			xyAxis.add(Float.parseFloat(coordinate.getLongitude()));
			xyAxis.add(Float.parseFloat(coordinate.getLatitude()));

			geoCoord.put(exportCity, xyAxis);
			if (flag) {
				topCityData.put("max", countryDataList.get(0).getSumNum_());
				topCityData.put("min",
						countryDataList.get(countryDataList.size() - 1)
								.getSumNum_());
			}
			else {
				topCityData.put("max", countryDataList.get(0).getSumNum());
				topCityData.put("min",
						countryDataList.get(countryDataList.size() - 1)
								.getSumNum());
			}
			topCityDataArray.add(topCityData);

			result.put("geoCoord", geoCoord);
			result.put("topCityArray", topCityArray);
			result.put("topCityDataArray", topCityDataArray);
			result.put("topCitySelected", topCitySelected);
		}

		return result;
	}

	private JSONObject getWorldExportPriceData(String exportCity,
			List<Ana2ndData> countryDataList, int topN) {
		JSONObject result = new JSONObject();
		if (countryDataList != null && countryDataList.size() > 0) {
			result.put("max", countryDataList.get(0).getSumPrice_());
			result.put("min", countryDataList.get(countryDataList.size() - 1)
					.getSumPrice_());
			JSONObject geoCoord = new JSONObject();

			JSONArray topCityArray = new JSONArray();
			topCityArray.add(exportCity);
			JSONObject topCitySelected = new JSONObject();
			topCitySelected.put(exportCity, true);
			JSONArray topCityDataArray = new JSONArray();
			JSONObject topCityData = new JSONObject();
			topCityData.put("cityName", exportCity);

			JSONArray markLineDataArr = new JSONArray();
			JSONArray markPointDataArr = new JSONArray();
			for (int i = 0; i < topN; i++) {
				Ana2ndData ana2ndData = countryDataList.get(i);
				String countryCode = ana2ndData.getMainCode();
				ConfCountry confCountry = cdc.getCountry(countryCode);
				if (null == confCountry) {
					continue;
				}
				Coordinate coordinate = cdc.getCoordinate(
						confCountry.getName(), confCountry.getMarkCity());
				if (null == coordinate) {
					continue;
				}
				// 纬度和经度（latitude and longitude）
				// 如{'Islands':[113.95, 22.26]}，香港离岛区名称显示定位到东经113.95，北纬22.26上
				JSONArray xyAxis = new JSONArray();
				xyAxis.add(Float.parseFloat(coordinate.getLongitude()));
				xyAxis.add(Float.parseFloat(coordinate.getLatitude()));
				geoCoord.put(confCountry.getName(), xyAxis);

				JSONArray markLineData = new JSONArray();
				JSONObject markPointData = new JSONObject();

				markPointData.put("name", confCountry.getName());
				markPointData.put("value", ana2ndData.getSumPrice_());

				JSONObject markLine1 = new JSONObject();
				markLine1.put("name", exportCity);

				JSONObject markLine2 = new JSONObject();
				markLine2.put("name", confCountry.getName());
				markLine2.put("value", ana2ndData.getSumPrice_());

				markLineData.add(markLine1);
				markLineData.add(markLine2);

				markLineDataArr.add(markLineData);
				markPointDataArr.add(markPointData);
			}

			topCityData.put("markPointDataArr", markPointDataArr);
			topCityData.put("markLineDataArr", markLineDataArr);

			Coordinate coordinate = cdc.getCoordinate("中国", exportCity);
			JSONArray xyAxis = new JSONArray();
			xyAxis.add(Float.parseFloat(coordinate.getLongitude()));
			xyAxis.add(Float.parseFloat(coordinate.getLatitude()));

			geoCoord.put(exportCity, xyAxis);

			topCityData.put("max", countryDataList.get(0).getSumPrice_());
			topCityData.put("min",
					countryDataList.get(countryDataList.size() - 1)
							.getSumPrice_());

			topCityDataArray.add(topCityData);

			result.put("geoCoord", geoCoord);
			result.put("topCityArray", topCityArray);
			result.put("topCityDataArray", topCityDataArray);
			result.put("topCitySelected", topCitySelected);
		}

		return result;
	}

	private JSONObject getThisTotalData(Ana1stData yearData,
			Ana1stData monthData, Ana1stData dayData, String dayFlag) {
		JSONObject result = new JSONObject();
		// String dayFlag = DateUtils.format(DateConstants.FORMAT_YYYY_MM_DD);
		BigDecimal todayCountNum = new BigDecimal(0);
		if (null != this.result
				&& null != this.result.getJSONObject("ThisTotalData")
				&& null != this.result.getJSONObject("ThisTotalData")
						.getBigDecimal("dayExportCount")) {
			todayCountNum = this.result.getJSONObject("ThisTotalData")
					.getBigDecimal("dayExportCount");
		}
		if (dayData != null) {
			result.put("dayExportCount", dayData.getSumNum());
			result.put("dayExportCountPrice", dayData.getSumPrice_());
		}
		else {
			result.put("dayExportCount", new BigDecimal(0));
			result.put("dayExportCountPrice", new BigDecimal(0));
		}

		if (monthData != null) {
			result.put("monthExportCount", monthData.getSumNum());
			result.put("monthExportCountPrice", monthData.getSumPrice_());
		}
		else {
			result.put("monthExportCount", new BigDecimal(0));
			result.put("monthExportCountPrice", new BigDecimal(0));
		}

		if (yearData != null) {
			result.put("yearExportCount", yearData.getSumNum());
			result.put("yearExportCountPrice", yearData.getSumPrice_());
		}
		else {
			result.put("yearExportCount", new BigDecimal(0));
			result.put("yearExportCountPrice", new BigDecimal(0));
		}

		// TODO 商家数待实现
		result.put("shopCount",
				cdc.getParamsMap().get(ParamFlag.SHOP_COMPANY.getFlag()).size());

		// 查看平均算的日期
		int day = DateUtils.getCurrentDay(dayFlag);
		// 指定日期和当前日期是否同年同月
		boolean isSameMonth = DateUtils.isSameMonth(dayFlag);
		// 获取总票数
		BigDecimal totalAllNum = result.getBigDecimal("monthExportCount");
		// 如果指定日期和当前日期是同年同月，则使用当月总数减去当天总数，当天的数据不能作为统计值
		if (isSameMonth && null != todayCountNum) {
			totalAllNum = totalAllNum.subtract(todayCountNum);
		}
		long agvMCount = 0L;
		// 当月首日出口包裹数为0，不需要统计月平均值
		if (day > 1) {
			agvMCount = totalAllNum.intValue() / (day - 1);
		}
		// TODO 月平均数待实现
		result.put("agvMCount", agvMCount);
		result.put("totalNum", totalNum);
		result.put("totalPrice", totalPrice);
		return result;
	}

	/**
	 * 
	 * setTotalData(这里用一句话描述这个方法的作用) <br>
	 * (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param yearFlag
	 * @param monthFlag
	 * @exception
	 * @since 2016年1月4日 下午7:35:24
	 * @author 徐韶兵
	 */
	private void setTotalData(String yearFlag, String monthFlag) {
		this.recentlyData = new JSONObject();
		// 查询当年所有二维数据
		Map<DimensionFlag, List<Ana2ndData>> yearTopNDataMap = govAnalysisService
				.queryTopNData(null, yearFlag, 10, "SUM_NUM",
						DimensionFlag.GOODS_TYPE, DimensionFlag.LOGIS_TYPE,
						DimensionFlag.SHOPS_COMPANY);
		Map<DimensionFlag, List<Ana2ndData>> yearCountryTopNDataMap = govAnalysisService
				.queryTopNData(null, yearFlag, 10, "SUM_NUM",
						DimensionFlag.EXPORT_COUNTRY, DimensionFlag.GEP_COUNTRY);
		Map<DimensionFlag, List<Ana2ndData>> yearCountryTopNPriceDataMap = govAnalysisService
				.queryTopNData(null, yearFlag, 14, "SUM_PRICE",
						DimensionFlag.EXPORT_COUNTRY);
		Map<DimensionFlag, List<Ana2ndData>> yearCategoryTopNDataMap = govAnalysisService
				.queryTopNData(null, yearFlag, 5, "SUM_PRICE",
						DimensionFlag.M_CATEGORY, DimensionFlag.S_CATEGORY);

		// 设置当年的国家排名数据
		recentlyData.put("CountryTopNData_Y",
				yearCountryTopNDataMap.get(DimensionFlag.EXPORT_COUNTRY));
		recentlyData.put("CountryTopNPriceData_Y",
				yearCountryTopNPriceDataMap.get(DimensionFlag.EXPORT_COUNTRY));
		// 设置当年的电商企业排名数据
		recentlyData.put("ShopCompanyTopNData_Y",
				yearTopNDataMap.get(DimensionFlag.SHOPS_COMPANY));

		// 设置当年出口国家地图数据
		recentlyData.put(
				"WorldExportData_Y",
				getWorldExportData("义乌", yearCountryTopNDataMap
						.get(DimensionFlag.EXPORT_COUNTRY), 10, true));
		recentlyData.put(
				"GepExportData_Y",
				getWorldExportData("义乌",
						yearCountryTopNDataMap.get(DimensionFlag.GEP_COUNTRY),
						10, false));
		recentlyData.put(
				"GepExportData_Y",
				getWorldExportData("义乌",
						yearCountryTopNDataMap.get(DimensionFlag.GEP_COUNTRY),
						10, false));

		recentlyData.put(
				"WorldExportPriceData_Y",
				getWorldExportPriceData("义乌", yearCountryTopNPriceDataMap
						.get(DimensionFlag.EXPORT_COUNTRY), 14));

		// 设置当年出口商品品类饼图数据
		recentlyData.put("GoodsPieData_Y",
				getGoodsPieData(yearTopNDataMap.get(DimensionFlag.GOODS_TYPE)));

		recentlyData.put("Mcategory_Y",
				getMCategoryData(yearCategoryTopNDataMap
						.get(DimensionFlag.M_CATEGORY)));

		recentlyData.put("Scategory_Y",
				getSCategoryData(yearCategoryTopNDataMap
						.get(DimensionFlag.S_CATEGORY)));

		// 设置当年出口物流产品类型饼图数据
		recentlyData.put("LogisTypePieData_Y",
				getLogisTypePieData(yearTopNDataMap
						.get(DimensionFlag.LOGIS_TYPE)));

		// 近一年数据查询
		List<Ana1stData> monthRecentlyDataList = govAnalysisService
				.queryMonthRecentlyData(
						DateUtils.getSpecifiedMonthBefore(monthFlag, 11),
						monthFlag);

		// 9610邮政监管中心

		List<Ana2ndData> recent12MonthData = govAnalysisService
				.queryMonthRecentlyData(
						DateUtils.getSpecifiedMonthBefore(monthFlag, 11),
						monthFlag, AnaTable.TB_ANA_DATA_2ND_GOV_M, "1",
						DimensionFlag.LOGIS_TYPE);
		recentlyData.put("Recently12Data0",
				getRecently12Data(monthFlag, recent12MonthData, 11, true));

		List<Ana2ndData> recent12MonthData1 = govAnalysisService
				.queryMonthRecentlyData(
						DateUtils.getSpecifiedMonthBefore(monthFlag, 11),
						monthFlag, AnaTable.TB_ANA_DATA_2ND_GOV_M, "3",
						DimensionFlag.LOGIS_TYPE);
		recentlyData.put("Recently12Data1",
				getRecently12Data(monthFlag, recent12MonthData1, 11, true));
		// 义新欧
		recentlyData.put("Recently12Data2",
				getRecently12Data(monthFlag, null, 11, false));
		// 近一年出口数据
		recentlyData.put("RecentlyMonthData",
				getRecentlyMonthData(monthFlag, monthRecentlyDataList, 11));

		recentlyData
				.put("RecentlyMonthPriceData",
						getRecentlyMonthPriceData(monthFlag,
								monthRecentlyDataList, 11));
		List<Ana2ndData> recent12MonthGepData = govAnalysisService
				.queryTotalData(
						DateUtils.getSpecifiedMonthBefore(monthFlag, 11),
						monthFlag, AnaTable.TB_ANA_DATA_2ND_GOV_M, null,
						DimensionFlag.GEP_LOGIS_CODE);
		recentlyData
				.put("Recently12GepData",
						getRecently12GepData(monthFlag, recent12MonthGepData,
								11, true));

	}

	/**
	 * updateDate(这里用一句话描述这个方法的作用) <br>
	 * (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param sessionId
	 * @param dateFlag
	 * @return
	 * @exception
	 * @since 2015年10月30日 上午10:09:41
	 * @author 徐韶兵
	 */
	public boolean updateDate(final String sessionId, final String dateFlag) {
		// 默认一分钟
		int exp = 60 * 60;
		return this.updateDate(sessionId, dateFlag, exp);
	}

	/**
	 * 更新数据
	 * 
	 * @param sessionId
	 * @param dateFlag
	 * @param exp
	 *            过期时间
	 * @return
	 */
	public boolean updateDate(final String sessionId, final String dateFlag,
			final int exp) {
		boolean flag = false;
		flag = redisTemplate.execute(new RedisCallback<Boolean>() {
			public Boolean doInRedis(RedisConnection connection)
					throws DataAccessException {
				byte[] keyb = jdkSerializationRedisSerializer
						.serialize(sessionId);
				byte[] valueb = jdkSerializationRedisSerializer
						.serialize(dateFlag);
				connection.setEx(keyb, exp, valueb);
				return true;
			}
		});

		return flag;
	}

	/**
	 * removeSession(这里用一句话描述这个方法的作用) <br>
	 * (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param sessionId
	 * @return
	 * @exception
	 * @since 2015年10月30日 上午10:09:47
	 * @author 徐韶兵
	 */
	public boolean removeSession(final String sessionId) {
		Long i = redisTemplate.execute(new RedisCallback<Long>() {
			public Long doInRedis(RedisConnection connection)
					throws DataAccessException {
				byte[] keyb = jdkSerializationRedisSerializer
						.serialize(sessionId);
				return connection.del(keyb);
			}
		});
		if (i > 0) {
			return true;
		}
		else {
			return false;
		}
	}

	/**
	 * getDateFlag(这里用一句话描述这个方法的作用) <br>
	 * (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param sessionId
	 * @return
	 * @exception
	 * @since 2015年10月30日 上午10:10:16
	 * @author 徐韶兵
	 */
	public String getDateFlag(final String sessionId) {
		try {
			return redisTemplate.execute(new RedisCallback<String>() {

				@Override
				public String doInRedis(RedisConnection connection)
						throws DataAccessException {
					byte[] keyb = jdkSerializationRedisSerializer
							.serialize(sessionId);
					return (String) jdkSerializationRedisSerializer
							.deserialize(connection.get(keyb));
				}
			});
		}
		catch (Exception e) {
			logger.error("Failed to get Cache date from redis, occour exception:"
					+ e.toString());
			return null;
		}

	}

	public ConfDataCacheService getCdc() {
		return cdc;
	}

	public void setCdc(ConfDataCacheService cdc) {
		this.cdc = cdc;
	}

	public GovAnalysisService getGovAnalysisService() {
		return govAnalysisService;
	}

	public void setGovAnalysisService(GovAnalysisService govAnalysisService) {
		this.govAnalysisService = govAnalysisService;
	}

	public static void main(String[] args) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM月dd日");
		SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
		System.out.println(dateFormat.format(new Date()));
		String day = "2016-02-29";
		Date date = null;
		try {
			date = dateFormat1.parse(day);
		}
		catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(date);
	}
}
